//
//  QTCircleViewController.m
//  QTLifeCircle
//
//  Created by 徐腾 on 2017/1/15.
//  Copyright © 2017年 徐腾. All rights reserved.
//

#import "QTCircleViewController.h"
#import "QTFlowLayout.h"
#import <Masonry.h>
#import "QTCircleCell.h"
#import "HMAdditions.h"
#import "QTAFNetworking.h"
#import <YYModel.h>
#import "QTCircleModel.h"
#import "QTCircleWebVC.h"
#import "QTPublicNavigationVC.h"

static NSString *cellID = @"cellID";

@interface QTCircleViewController () <UICollectionViewDataSource,UICollectionViewDelegate>

/** 数据源数组 */
@property (nonatomic,strong) NSArray *circleList;

/** <#name#> */
@property (nonatomic,strong) UICollectionView *collectionView;

@end

@implementation QTCircleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //加载数据
    [self loadData];
    
    //设置界面
    [self setupUI];
    
}


/**
 加载数据
 */
- (void)loadData {
    
    [QTAFNetworking getUrlString:@"http://iosapi.itcast.cn/life/toolsListData.json.php" withParam:nil withSuccessBlock:^(NSDictionary * data) {
        
        NSArray *arr = data[@"tools_list"];
        
        self.circleList = [NSArray yy_modelArrayWithClass:[QTCircleModel class] json:arr];
        
//        NSLog(@"%@",self.circleList);
        [self.collectionView reloadData];
        
    } withFailedBlock:^(NSError *error) {
        
    } withErrorBlock:^(NSString *message) {
        
    }];
}


/**
 设置界面
 */
- (void)setupUI {
    
    //创建流布局
    QTFlowLayout *flowLayout = [[QTFlowLayout alloc] init];
    
    //创建collectionView
    UICollectionView *collView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    self.collectionView = collView;
    //设置数据源对象
    collView.dataSource = self;
    
    //设置代理对象
    collView.delegate = self;
    
    //设置颜色
    collView.backgroundColor = [UIColor whiteColor];
    
    //禁用滚动条
    collView.showsVerticalScrollIndicator = NO;
    collView.showsHorizontalScrollIndicator = NO;
    
    //注册cell
    [collView registerClass:[QTCircleCell class] forCellWithReuseIdentifier:cellID];
    
    //添加到父视图上
    [self.view addSubview:collView];
    
    //设置约束
    [collView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
}


#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.circleList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //创建cell
    QTCircleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
    //设置cell的背景颜色
//    cell.backgroundColor = [UIColor hm_randomColor];
    cell.model = self.circleList[indexPath.row];
    
    return cell;
}


#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //获取模型
    QTCircleModel *model = self.circleList[indexPath.row];
    
    QTCircleWebVC *webVC = [[QTCircleWebVC alloc] init];
    webVC.urlStr = model.link;
    
    [self.navigationController pushViewController:webVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
